//此js是为了做兼容JQUERY而设计的。
//工程名
var contextName;
//工程lib依赖
var mapperLibs={
    "jQuery":{url:contextName+'/lib/jquery/jquery.min.js',tagName:'script'},
    "util":{url:contextName+'/lib/common/util.js',tagName:'script'},
    "clipboard":{url:contextName+'/lib/jquery/clipboard.min.js',tagName:'script'},
    "tools":[
        {url:contextName+'/js/common/tools.js',tagName:'script'},
        {url:contextName+'/css/common/common.css',tagName:'link'}
    ],
    "swiDialog":[
        {url:contextName+'/lib/swi/common/move.js',tagName:'script'},
        {url:contextName+'/lib/swi/dialog/s_dialog.js',tagName:'script'},
        {url:contextName+'/lib/swi/dialog/s_dialog.css',tagName:'link'},
        {url:contextName+'/lib/swi/common/common.css',tagName:'link'}
    ],
    "swiWindow":[
        {url:contextName+'/lib/swi/common/move.js',tagName:'script'},
        {url:contextName+'/lib/swi/window/s_window.js',tagName:'script'},
        {url:contextName+'/lib/swi/window/s_window.css',tagName:'link'},
        {url:contextName+'/lib/swi/common/common.css',tagName:'link'},
        {url:contextName+'/lib/swi/process/process.js',tagName:'script'},
        {url:contextName+'/lib/swi/process/process.css',tagName:'link'}
    ],
    "swiSourceData":{url:contextName+'/lib/swi/window/source_data.js',tagName:'script'},
    "swiPage":[
        {url:contextName+'/lib/swi/split_page/s_page.js',tagName:'script'},
        {url:contextName+'/lib/swi/split_page/s_page.css',tagName:'link'}
    ],
    "swiProcess":[
        {url:contextName+'/lib/swi/process/process.js',tagName:'script'},
        {url:contextName+'/lib/swi/process/process.css',tagName:'link'}
    ],
    "rightMenu":[
        {url:contextName+'/lib/swi/right_menu/s_script.js',tagName:'script'},
        {url:contextName+'/lib/swi/right_menu/s_style.css',tagName:'link'}
    ],
    "messageContent":[
        {url:contextName+'/lib/swi/message/message-content.js',tagName:'script'}
    ],
    "codemirror":[
        {url:contextName+'/lib/codemirror/theme/liquibyte.css',tagName:'link'},
        {url:contextName+'/lib/codemirror/lib/codemirror.css',tagName:'link'},
        {url:contextName+'/lib/codemirror/addon/hint/show-hint.css',tagName:'link'},
        {url:contextName+'/lib/codemirror/lib/codemirror.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/selection/active-line.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/edit/closebrackets.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/edit/closetag.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/edit/continuelist.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/edit/matchbrackets.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/edit/matchtags.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/edit/trailingspace.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/hint/javascript-hint.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/addon/hint/show-hint.js',tagName:'script'},
        {url:contextName+'/lib/codemirror/mode/javascript/javascript.js',tagName:'script'}
    ]
};
// window.onload=function(){
//     appendLib("script","lib");
// }
var whenLoaded;
var $=function(w){
    whenLoaded=w;
},jQuery=$;
function appendLib(tagName,attrName){
    alert("???????")
    var libs = document.getElementsByTagName(tagName);
    var orderLibs=[];
    if(libs){
        for(var i in libs){
            var lib=libs[i];
            if(!lib.getAttribute) continue;
            var includes = lib.getAttribute(attrName);
            if(includes){
                var incs = includes.split(",");
                if(incs){
                    for(var k in incs){
                        var p = incs[k];
                        var attrs=mapperLibs[p];
                        if(attrs){
                            if(attrs.constructor.toString().indexOf("Array")==-1){
                                var temp = attrs;
                                attrs=[];
                                attrs.push(temp);
                            }
                            for(var j in attrs){
                                var attr = attrs[j];
                                var element;
                                if(attr.tagName==='link'){
                                    element = document.createElement(attr.tagName);
                                    element.setAttribute("rel","stylesheet");
                                    element.setAttribute("type","text/css");
                                    element.setAttribute("href",attr.url);
                                    document.getElementsByTagName("html")[0].appendChild(element);
                                }else if(attr.tagName==='script'){
                                    // element.onload=function(){success[index]=true;if(startCheck) completeLoad(success)}
                                    // element.onreadystatechange=function(){if (this.readyState == 'complete'){success[index]=true;if(startCheck) completeLoad(success)}}
                                    orderLibs.push(attr.url);
                                }
                            }
                        }
                    }
                }
                lib.remove();
            }
        }
        completeLoad(orderLibs);
    }
}
function completeLoad(array,index){
    index=index==null?0:index;
    if(index>=array.length){
        if(typeof(whenLoaded)==='function'){
            whenLoaded();
        }
        return;
    }
    var element = document.createElement("script");
    element.setAttribute("src",array[index++]);
    element.onload=function(){completeLoad(array,index)}
    element.onreadystatechange=function(){if (this.readyState == 'complete'){completeLoad(array,index)}}
    document.getElementsByTagName("html")[0].appendChild(element);
}

function loadNewJs(array,index,callback){
    if(typeof(index)==='function'){
        callback=index;
        index=0;
    }
    if(typeof(array)==='string'){
        var t=array;
        array=[];
        array.push(t);
    }
    index=index==null?0:index;
    if(index>=array.length){
        if(typeof(callback)==='function'){
            callback();
        }
        return;
    }
    var element = document.createElement("script");
    element.setAttribute("src",array[index++]);
    element.onload=function(){loadNewJs(array,index,callback)}
    element.onreadystatechange=function(){if (this.readyState == 'complete'){loadNewJs(array,index,callback)}}
    document.getElementsByTagName("html")[0].appendChild(element);
}

appendLib("script","lib");